package com.martin.lib_base.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.martin.lib_base.interfaces.ILoading
import com.martin.lib_base.interfaces.IRelease
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.model.LoadingModel
import com.martin.lib_base.utils.ReleaseUtil
import kotlin.properties.Delegates

abstract class BaseFragment<T : ViewDataBinding> : Fragment(), ILoading,IRelease {

    protected var binding: T by Delegates.notNull()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, layoutId(), container, false)

        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (showLife()) {
            loge("${getClassName()} : onViewCreated")
        }
        initOnViewCreated()
    }

    /**
     * Fragment布局ID
     */
    abstract fun layoutId(): Int

    /**
     * 初始化Fragment内容
     */
    abstract fun initOnViewCreated()

    //加载弹窗
    private val _loadingModel: LoadingModel by lazy {
        LoadingModel(requireContext())
    }

    /**
     * 展示加载弹窗
     */
    override fun showLoading() {
        if (!requireActivity().isFinishing) {
            _loadingModel.showLoading()
        }
    }

    /**
     * 隐藏加载弹窗
     */
    override fun dismissLoading() {
        _loadingModel.dismissLoading()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (showLife()) {
            loge("${getClassName()} : onCreate")
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (showLife()) {
            loge("${getClassName()} : onAttach")
        }
    }

    override fun onStart() {
        super.onStart()
        if (showLife()) {
            loge("${getClassName()} : onStart")
        }
    }

    override fun onResume() {
        super.onResume()
        if (showLife()) {
            loge("${getClassName()} : onResume")
        }
    }

    override fun onPause() {
        super.onPause()
        if (showLife()) {
            loge("${getClassName()} : onPause")
        }
    }

    override fun onStop() {
        super.onStop()
        if (showLife()) {
            loge("${getClassName()} : onStop")
        }
    }

    override fun onDetach() {
        super.onDetach()
        if (showLife()) {
            loge("${getClassName()} : onDetach")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (showLife()) {
            loge("${getClassName()} : onDestroy")
        }
        release()
    }

    override fun release() {
        dismissLoading()
        ReleaseUtil.release(this)
    }

    private fun getClassName() = javaClass.simpleName

    /**
     * 展示生命周期
     */
    open fun showLife() = false
}